package cn.aloxc.stock
import cn.aloxc.stock.context.SysContext;
import java.text.SimpleDateFormat;

class FundstatController {
    
    def index = {
        redirect action:"sdd", params:params 
    }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ fundstatInstanceList: Fundstat.list( params ), fundstatInstanceTotal: Fundstat.count() ]
    }
    def sdd = {
      params.max = params.max ? (params.max.toInteger() > 90 ? 90 : params.max.toInteger()) : SysContext.FUND_SDD_MAX;
      params.a = params.a ? SysContext.FUND_ATYPE[(params.a.toInteger() > SysContext.FUND_ATYPE.size() ? 0 : params.a.toInteger())] : SysContext.FUND_ATYPE[0];
      params.p = params.p ? SysContext.FUND_PERIOD[(params.p.toInteger() > SysContext.FUND_PERIOD.size() ? 0 : params.p.toInteger())] : SysContext.FUND_PERIOD[0];
      params.s = params.s ? params.s.toInteger() : SysContext.FUND_SDD_DAYS;
      def curDate = new Date() - params.s;
      def fundstatdataMap = new TreeMap();
      def sdf = new SimpleDateFormat("yyMMdd");
      //render params;
      //return;
      0.step(params.s,1){
        curDate++;
        //println sdf.format(curDate);
        curDate.setHours(0);
        curDate.setMinutes(0);
        curDate.setSeconds(0);
        def criteria = Fundstat.createCriteria();
        def cList,key;
        cList = criteria.list{
          eq("period",params.p);
          eq("atype",params.a);
          eq("statdate",curDate);
          maxResults(params.max);
          order("id","asc")
        };
        def fList = [];
        cList.each{
           fList.add(it);
        };
        key = sdf.format(curDate);
        if(fList.size()>0)
        {
          fundstatdataMap.put(key,fList)
        }
      };
      render view:'sdd', model:[fundstatInstanceMapList:fundstatdataMap]
    }

    def show = {
      redirect action:"sdd", params:params
      /*
        def fundstatInstance = Fundstat.get( params.id )

        if(!fundstatInstance) {
            flash.message = "Fundstat not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            [ fundstatInstance : fundstatInstance ]
        }
        */
    }

    def delete = {
      redirect action:"sdd", params:params
      /*      
        def fundstatInstance = Fundstat.get( params.id )
        if(fundstatInstance) {
            try {
                fundstatInstance.delete(flush:true)
                flash.message = "Fundstat ${params.id} deleted"
                redirect(action:"list")
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Fundstat ${params.id} could not be deleted"
                redirect(action:"show",id:params.id)
            }
        }
        else {
            flash.message = "Fundstat not found with id ${params.id}"
            redirect(action:"list")
        }
        */
    }

    def edit = {
      redirect action:"sdd", params:params
      /*
        def fundstatInstance = Fundstat.get( params.id )

        if(!fundstatInstance) {
            flash.message = "Fundstat not found with id ${params.id}"
            redirect action:'list'
        }
        else {
            return [ fundstatInstance : fundstatInstance ]
        }
        */
    }

    def update = {
      redirect action:"sdd", params:params
      /*
        def fundstatInstance = Fundstat.get( params.id )
        if(fundstatInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(fundstatInstance.version > version) {
                    
                    fundstatInstance.errors.rejectValue("version", "fundstat.optimistic.locking.failure", "Another user has updated this Fundstat while you were editing.")

                    render view:'edit', model:[fundstatInstance:fundstatInstance]
                    return
                }
            }
            fundstatInstance.properties = params
            if(!fundstatInstance.hasErrors() && fundstatInstance.save()) {
                flash.message = "Fundstat ${params.id} updated"

                redirect action:'show', id:fundstatInstance.id
            }
            else {
                render view:'edit', model:[fundstatInstance:fundstatInstance]
            }
        }
        else {
            flash.message = "Fundstat not found with id ${params.id}"
            redirect action:'list'
        }
        */
    }

    def create = {
      redirect action:"sdd", params:params
      /*
        def fundstatInstance = new Fundstat()
        fundstatInstance.properties = params
        return ['fundstatInstance':fundstatInstance]
        */
    }

    def save = {
      redirect action:"sdd", params:params
      /*
        def fundstatInstance = new Fundstat(params)
        if(fundstatInstance.save(flush:true)) {
            flash.message = "Fundstat ${fundstatInstance.id} created"

            redirect action:"show", id:fundstatInstance.id
        }
        else {
            render view:'create', model:[fundstatInstance:fundstatInstance]
        }
        */
    }

}
